package com.leadingsoft.pwxk.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leadingsoft.pwxk.dto.DrainPositionDescDto;
import com.leadingsoft.pwxk.dto.DrainPositionDto;
import com.leadingsoft.pwxk.enums.DrainType;
import com.leadingsoft.pwxk.model.QDrainBaseInfo;
import com.leadingsoft.pwxk.model.QDrainInfoAirOrgOne;
import com.leadingsoft.pwxk.model.QDrainInfoAirOrgTwo;
import com.leadingsoft.pwxk.model.QDrainInfoWaterApplyOne;
import com.leadingsoft.pwxk.model.QDrainInfoWaterApplyThr;
import com.leadingsoft.pwxk.model.QDrainInfoWaterApplyTwo;
import com.leadingsoft.pwxk.model.QPwBaseInfo;
import com.leadingsoft.pwxk.model.QWaterDrainBaseInfoOne;
import com.leadingsoft.pwxk.service.DrainBaseInfoService;
import com.querydsl.core.Tuple;
import com.querydsl.jpa.impl.JPAQuery;

/**
 * 
 * @author ysh
 * @desc 获取排放口位置信息
 */
@Service
public class DrainBaseInfoServiceImpl implements DrainBaseInfoService {

	@Autowired
	EntityManager em;

	QDrainBaseInfo qDrainBaseInfo = QDrainBaseInfo.drainBaseInfo;

	QDrainInfoAirOrgOne qDrainInfoAirOrgOne = QDrainInfoAirOrgOne.drainInfoAirOrgOne;
	QDrainInfoAirOrgTwo qDrainInfoAirOrgTwo = QDrainInfoAirOrgTwo.drainInfoAirOrgTwo;

	QWaterDrainBaseInfoOne qWaterDrainBaseInfoOne = QWaterDrainBaseInfoOne.waterDrainBaseInfoOne;
	QDrainInfoWaterApplyOne qDrainInfoWaterApplyOne = QDrainInfoWaterApplyOne.drainInfoWaterApplyOne;
	QDrainInfoWaterApplyTwo qDrainInfoWaterApplyTwo = QDrainInfoWaterApplyTwo.drainInfoWaterApplyTwo;
	QDrainInfoWaterApplyThr qDrainInfoWaterApplyThr = QDrainInfoWaterApplyThr.drainInfoWaterApplyThr;

	QPwBaseInfo qPwBaseInfo = QPwBaseInfo.pwBaseInfo;

	@Override
	public DrainPositionDto getPosition(String dataId) {
		DrainPositionDto drainPositionDto = new DrainPositionDto();
		// 获取大气
		getAirDrain(drainPositionDto, dataId);
		// 获取公司
		getGs(drainPositionDto, dataId);
		// 获取水
		getWaterDrain(drainPositionDto, dataId);
		return drainPositionDto;
	}

	private void getAirDrain(DrainPositionDto drainPositionDto, String dataId) {
		// 大气主要排放口
		getAirMainDrain(drainPositionDto, dataId);
		// 大气一般排放口
		getAirGeneral(drainPositionDto, dataId);
	}

	private void getAirMainDrain(DrainPositionDto drainPositionDto, String dataId) {
		JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
		query.select(qDrainBaseInfo.latitude, qDrainBaseInfo.longitude, qDrainBaseInfo.xkdraincode,
				qDrainBaseInfo.wrwname);
		query.from(qDrainBaseInfo).innerJoin(qDrainInfoAirOrgOne)
				.on(qDrainBaseInfo.dataid.eq(qDrainInfoAirOrgOne.dataid));
		query.where(
				qDrainBaseInfo.dataid.eq(dataId).and(qDrainBaseInfo.xkdraincode.eq(qDrainInfoAirOrgOne.xkdraincode)));
		getCommon(drainPositionDto, DrainType.AIR_MAIN, query);
	}

	private void getAirGeneral(DrainPositionDto drainPositionDto, String dataId) {
		JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
		query.select(qDrainBaseInfo.latitude, qDrainBaseInfo.longitude, qDrainBaseInfo.xkdraincode,
				qDrainBaseInfo.wrwname);
		query.from(qDrainBaseInfo).innerJoin(qDrainInfoAirOrgTwo)
				.on(qDrainBaseInfo.dataid.eq(qDrainInfoAirOrgTwo.dataid));
		query.where(
				qDrainBaseInfo.dataid.eq(dataId).and(qDrainBaseInfo.xkdraincode.eq(qDrainInfoAirOrgTwo.xkdraincode)));
		getCommon(drainPositionDto, DrainType.AIR_GENERAL, query);
	}

	private void getWaterDrain(DrainPositionDto drainPositionDto, String dataId) {
		// 废水主要排放口
		getWaterMain(drainPositionDto, dataId);
		// 废水一般排放口
		getWaterGeneral(drainPositionDto, dataId);
		// 废水设备车间排放口
		getWaterFac(drainPositionDto, dataId);
	}

	private void getWaterMain(DrainPositionDto drainPositionDto, String dataId) {
		JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
		query.select(qWaterDrainBaseInfoOne.latitude, qWaterDrainBaseInfoOne.longitude,
				qWaterDrainBaseInfoOne.xkdraincode);
		query.from(qWaterDrainBaseInfoOne).innerJoin(qDrainInfoWaterApplyOne)
				.on(qWaterDrainBaseInfoOne.dataid.eq(qDrainInfoWaterApplyOne.dataid));
		query.where(qWaterDrainBaseInfoOne.dataid.eq(dataId)
				.and(qWaterDrainBaseInfoOne.xkdraincode.eq(qDrainInfoWaterApplyOne.xkdraincode)));
		getCommon(drainPositionDto, DrainType.WATER_MAIN, query);
	}

	private void getWaterGeneral(DrainPositionDto drainPositionDto, String dataId) {
		JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
		query.select(qWaterDrainBaseInfoOne.latitude, qWaterDrainBaseInfoOne.longitude,
				qWaterDrainBaseInfoOne.xkdraincode);
		query.from(qWaterDrainBaseInfoOne).innerJoin(qDrainInfoWaterApplyTwo)
				.on(qWaterDrainBaseInfoOne.dataid.eq(qDrainInfoWaterApplyTwo.dataid));
		query.where(qWaterDrainBaseInfoOne.dataid.eq(dataId)
				.and(qWaterDrainBaseInfoOne.xkdraincode.eq(qDrainInfoWaterApplyTwo.xkdraincode)));
		getCommon(drainPositionDto, DrainType.WATER_GENERAL, query);
	}

	private void getWaterFac(DrainPositionDto drainPositionDto, String dataId) {
		JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
		query.select(qWaterDrainBaseInfoOne.latitude, qWaterDrainBaseInfoOne.longitude,
				qWaterDrainBaseInfoOne.xkdraincode);
		query.from(qWaterDrainBaseInfoOne).innerJoin(qDrainInfoWaterApplyThr)
				.on(qWaterDrainBaseInfoOne.dataid.eq(qDrainInfoWaterApplyThr.dataid));
		query.where(qWaterDrainBaseInfoOne.dataid.eq(dataId)
				.and(qWaterDrainBaseInfoOne.xkdraincode.eq(qDrainInfoWaterApplyThr.xkdraincode)));
		getCommon(drainPositionDto, DrainType.WATER_FAC, query);
	}

	private void getCommon(DrainPositionDto drainPositionDto, DrainType drainType, JPAQuery<Tuple> query) {
		List<DrainPositionDescDto> list = new ArrayList<DrainPositionDescDto>();
		List<Tuple> fetch = query.distinct().fetch();
		fetch.forEach(t -> {
			DrainPositionDescDto airDto = new DrainPositionDescDto();
			airDto.setName(t.get(2, String.class));
			airDto.setLongitude(t.get(1, String.class));
			airDto.setLatitude(t.get(0, String.class));
			if (StringUtils.isNotBlank(airDto.getLatitude()) && StringUtils.isNotBlank(airDto.getLongitude())) {
				list.add(airDto);
			}

		});
		switch (drainType) {
		case AIR_GENERAL:
			drainPositionDto.setAirGeneral(list);
			break;
		case AIR_MAIN:
			drainPositionDto.setAirMain(list);
			break;
		case WATER_MAIN:
			drainPositionDto.setWaterMain(list);
			break;
		case WATER_GENERAL:
			drainPositionDto.setWaterGeneral(list);
			break;
		case WATER_FAC:
			drainPositionDto.setWaterFac(list);
			break;
		default:
			break;
		}

	}

	private void getGs(DrainPositionDto drainPositionDto, String dataId) {
		JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
		query.select(qPwBaseInfo.latitude, qPwBaseInfo.longitude, qPwBaseInfo.opeaddress);
		query.from(qPwBaseInfo).where(qPwBaseInfo.dataid.eq(dataId));
		Tuple fetchOne = query.fetchFirst();
		if (fetchOne != null) {
			DrainPositionDescDto drainPositionDescDto = new DrainPositionDescDto();
			drainPositionDescDto.setLatitude(fetchOne.get(qPwBaseInfo.latitude));
			drainPositionDescDto.setLongitude(fetchOne.get(qPwBaseInfo.longitude));
			drainPositionDescDto.setName(fetchOne.get(qPwBaseInfo.opeaddress));
			drainPositionDto.setGs(drainPositionDescDto);
		}
	}
}
